﻿using B.S.Production.Domain.Entities;
using B.S.Production.ErrorCode;
using B.S.Production.Read.API.Application.Command.GetMaterialsRequiredCommand;
using MediatR;
using SqlSugar;

namespace B.S.Production.Read.API.Application.CommandHandler.GetMaterialsRequiredCommandHandler
{
    public class GetMaterialsRequiredCommandHandler : IRequestHandler<GetMaterialsRequiredCommand, ApiResult<List<MaterialsRequired>>>
    {
        private readonly ISqlSugarClient db;

        public GetMaterialsRequiredCommandHandler(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<ApiResult<List<MaterialsRequired>>> Handle(GetMaterialsRequiredCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<MaterialsRequired>> result = new ApiResult<List<MaterialsRequired>>();
            var list = await db.Queryable<MaterialsRequired>().ToListAsync();
            result.Data = list;
            if (result.Data != null)
            {
                result.Code = ApiEnums.查询成功;
                result.Msg = ApiEnums.查询成功.ToString();
            }
            else
            {
                result.Code = ApiEnums.查询失败;
                result.Msg = ApiEnums.查询失败.ToString();
            }
            return result;
        }
    }
}
